package com.ygqh.baby.service.impl;

import com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.dao.ProcessUserReportDao;
import com.ygqh.baby.model.ProcessUserReport;
import com.ygqh.baby.service.ProcessUserReportService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: sunshuo
 * @Date: 2019/9/18/018 14:04
 * @Version: 1.0
 */
@Service
public class ProcessUserReportServiceImpl implements ProcessUserReportService {

    @Autowired
    private ProcessUserReportDao processUserReportDao;

    @Override
    public ProcessUserReport findLastReport() {
        Map<String, Object> conditions = new HashMap<>(1);
        conditions.put("status", DataStatus.Valid);
        Map<String, String> orders = new HashMap<>(1);
        orders.put("reportDate", Sort.Direction.DESC.name());
        List<ProcessUserReport> pageEntityList =
                processUserReportDao.getPageEntityList(new QueryInfo(0, 1), conditions, orders);
        return CollectionUtils.isEmpty(pageEntityList) ? null : pageEntityList.get(0);
    }

    @Override
    public void saveBatch(List<ProcessUserReport> reportList) {
        if (CollectionUtils.isNotEmpty(reportList)) {
            processUserReportDao.saveBatch(reportList);
        }
    }

    @Override
    public void deleteByReportDate(Date startDate, Date endDate) {
        Map<String, Object> updateMap = new HashMap<>(2);
        updateMap.put("status", DataStatus.Delete.name());
        Map<String, Object> conditions = new HashMap<>(2);
        conditions.put("status", DataStatus.Valid);
        Map<String, Object> compareMap = new HashMap<>(1);
        Map<String, Object> dateMap = new HashMap<>(2);
        dateMap.put("gte", startDate);
        dateMap.put("lt", endDate);
        compareMap.put("reportDate", dateMap);
        conditions.put("compare", compareMap);
        processUserReportDao.updateEntities(updateMap, conditions);
    }

    @Override
    public ResultSet<ProcessUserReport> search(QueryInfo queryInfo, Date startDate, Date endDate) {
        Map<String, Object> conditions = new HashMap<>(2);
        conditions.put("status", DataStatus.Valid);
        if (startDate != null || endDate != null) {
            Map<String, Object> compareMap = new HashMap<>(1);
            Map<String, Object> dateMap = new HashMap<>(2);
            if (startDate != null) {
                dateMap.put("gte", startDate);
            }
            if (endDate != null) {
                dateMap.put("lt", endDate);
            }
            compareMap.put("reportDate", dateMap);
            conditions.put("compare", compareMap);
        }
        long total = processUserReportDao.getEntityCount(conditions);
        if (total == 0) {
            return new ResultSet<>();
        }
        Map<String, String> orders = new HashMap<>(1);
        orders.put("reportDate", Sort.Direction.DESC.name());
        List<ProcessUserReport> result = processUserReportDao.getPageEntityList(queryInfo, conditions, orders);
        return new ResultSet<>(total, result);
    }
}
